package com.fqh.controller;

import com.alipay.api.AlipayApiException;
import com.alipay.api.internal.util.AlipaySignature;
import com.fqh.config.AlipayConfig;
import com.fqh.entity.Login;
import com.fqh.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

@Scope(value = "prototype")
@Controller
public class PaySuccessController {
    @Autowired
    private OrderService orderService;

    @RequestMapping("success.do")
    public void paySuccess(HttpServletRequest request, HttpServletResponse response) throws IOException {
        Map<String, String> params = orderService.getPayRequestParam(request);

        //校验 验签
        // 判断是否真的支付成功
        try {
            boolean signVerified = AlipaySignature.rsaCheckV1(params, AlipayConfig.alipay_public_key, AlipayConfig.charset, AlipayConfig.sign_type);
            if (signVerified) {

                orderService.putInfo(request);
                orderService.deleteRedis(request);
                orderService.addOrderManage(request);

                orderService.updateSessionByPayInfo(request);
                response.sendRedirect("/Boheyuan_Portal_war_exploded/view/bill.html");
            } else {
                response.getWriter().write("fail");
            }
        } catch (AlipayApiException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } //调用SDK验证签名
    }

}
